home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / ROTATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-10  |  14.2 KB  |  556 lines

  1. /* interactive 3-d rotating cube
  2.    
  3.    allows user to choose various options while displaying 
  4.    a cube in 3 space
  5.  
  6.  
  7.    code thrown together on 1-13-94 just to see if it'd work
  8.  
  9.  
  10.  
  11.  
  12.  
  13.         jmb8302@cs.tamu.edu     Jeffrey Bilger
  14. */
  15.  
  16.  
  17. #include <osbind.h>
  18. #include <gemdefs.h>
  19. #include <math.h>
  20. #include <linea.h>
  21.  
  22. int pts[4][2] = { 
  23.     320, 050,
  24.     120, 150,
  25.     520, 150,
  26.     320, 050
  27. };
  28. lineaport *theport;
  29.  
  30.  
  31. #define M1 65536L
  32. #define MF 65536.0
  33. #define BITSH 16
  34. #define PI 205887L
  35. #define SPININC (PI >> 4)
  36. #define addpt(p1,p2,p3) \
  37.         { p3.x = p1.x + p2.x; \
  38.           p3.y = p1.y + p2.y; \
  39.           p4.z = p1.z + p2.z; }
  40.  
  41. #define cosf(x) (cos((double) x / M1))
  42. #define sinf(x) (sin((double) x / M1))
  43.  
  44. typedef struct {double x,y,z;} point3d;
  45.  
  46. #define perx(p) \
  47.         (perspective ? ((p.x/(p.z+pdist))*pdist):p.x)
  48. #define pery(p) \
  49.         (perspective ? ((p.y/(p.z+pdist))*pdist):p.y)
  50.  
  51.  
  52.  
  53. #define NPTS 64
  54. #define NLINES 64
  55. #define NFACES 64
  56. #define I 100.0
  57.  
  58.  
  59. point3d point_[NPTS] =
  60.         { -10.0,-10.0,10.0,   10.0,-10.0,10.0,  10.0,10.0,10.0,  -10.0,10.0,10.0,
  61.            -10.0,-10.0,-10.0, 10.0,-10.0,-10.0, 10.0,10.0,-10.0, -10.0,10.0,-10.0 },
  62.         drawpt1[NPTS],drawpt2[NPTS];
  63. long spininc = SPININC >> 2;
  64.  
  65. int drawclr,
  66.     white,
  67.     glasses = 0,
  68.     perspective = 0,
  69.     npts  = 8,
  70.     linefrom[NLINES] = {0,1,2,3,0,4,7,1,5,6,6,5},
  71.     lineto[NLINES]   = {1,2,3,0,4,7,3,5,6,2,7,4},
  72.     from[NLINES]     = {0,1,1,5,4,7,0,3,2,6,1,0},
  73.     to[NLINES]       = {1,2,5,6,7,6,3,7,6,7,0,4},
  74.     nlines = 12;
  75.  
  76. double pdist=200.0;
  77.  
  78. unsigned int pause = 8092;
  79.  
  80. int x_offset=320,y_offset=100;
  81. double z_offset=1.0;
  82.  
  83.  
  84. int contrl[12];
  85. int intin[256],  ptsin[256];
  86. int intout[256], ptsout[256];
  87.  
  88. #define REZ 1                    /* 1 med, 0 low */
  89. #define rnd(t) Random()%(t)   /*returns a number from 0 to (t-1) */
  90.  
  91. char     temp_[32000],                     /* Temp buffer where file is read in    */
  92.          *hld,
  93.          *iff_in, *iff_out;                  /* Pointers for DEGAS unpack() routine    */
  94.  
  95. #include <stdio.h>
  96. int contrl[12];
  97. int intin[256],  ptsin[256];
  98. int intout[256], ptsout[256];
  99.  
  100. char title[] ="rotate.pc2";
  101. int savepal[16],newpal[16],junkbuff[46],pal2[16];
  102. int filehandle,xoff,yoff,zoff;
  103. unsigned form[37];
  104.  
  105. main()
  106.  
  107. {
  108.     int save_rot;
  109.     char *scr1,*scr2,*screen,*temp;
  110.     int kill1;
  111.     char a,string[20];    
  112.       MFDB    source;
  113.     int        handle,status,x,y;
  114.     register int i,j;
  115.     point3d pointi;
  116.     int color =3;
  117.     double cos_2,sin_2;
  118.     char com;
  119.     int rect[4];
  120.     int pl0,pl1,pl2,pl3;
  121.     
  122.     /*
  123.         Start up ROM.
  124.     */
  125.     appl_init();
  126.     handle = open_workstation(&source);
  127.  
  128.     theport = a_init();     
  129.  
  130.    
  131.     pl0=theport -> plane0;
  132.     pl1=theport -> plane1;
  133.     pl2=theport -> plane2;
  134.     pl3=theport -> plane3;
  135.  
  136.  
  137.     theport -> plane0 = 0;
  138.     theport -> plane1 = 0;
  139.     theport -> plane2 = 0;
  140.     theport -> plane3 = 0;
  141.  
  142.  
  143.     cos_2 = cos( (double) (5.0*3.141596/180.0));  /* precompute values */
  144.     sin_2 = sin( (double) (5.0*3.141596/180.0));
  145.  
  146.    scr1 = malloc(32768+256);  /*allocate memory for 2nd screen */
  147.     if ((long) scr1 & 0xff)
  148.       scr1 = scr1 + (0x100 - (long)scr1 & 0xff);
  149.   scr2 = malloc(32768+256);  /*allocate memory for 3rd screen */
  150.     if ((long) scr2 & 0xff)
  151.       scr2 = scr2 + (0x100 - (long)scr2 & 0xff);
  152. screen = (char *)Physbase();
  153. read_stuff(title,scr1,1);  /* read main title screen onto the temp scr*/
  154. read_stuff(title,scr2,1);  /* read main title screen onto the temp scr*/
  155.  
  156.  
  157. Setpalette(newpal);        /* tel sys to use these colors! */
  158. Setscreen(scr1,scr1,-1);
  159.  
  160. graf_mouse(3,form);
  161.  
  162.  
  163.  
  164. for(i=0;i<npts;i++)
  165.    {
  166.    pointi = point_[i];
  167.    point_[i].y =( pointi.y * cos_2  +
  168.                 pointi.z * sin_2);
  169.    point_[i].z =( pointi.y * -sin_2 +
  170.                 pointi.z * cos_2);
  171.    }
  172.  
  173.  
  174. for(i=0;i<nlines;i++)  /* set up points-to-connect lookup table*/
  175. { drawpt1[i] = point_[linefrom[i]];
  176.   drawpt2[i] = point_[lineto[i]];  
  177. }
  178.  
  179.  
  180. com = 0x31;  /* start out spinning y */
  181.  
  182.  
  183. do{
  184.     vq_mouse(handle,&status,&x,&y);
  185.  if(status & 0x1)   com=pick(x,y);  /* left button pressed */    
  186.  
  187.  for(i=0;i<npts;i++)
  188.    {
  189.    pointi = point_[i];
  190.    
  191.     if(com == 0x31) {              /* spin y */
  192.                point_[i].x =( pointi.x * cos_2  -
  193.                               pointi.z * sin_2);
  194.                point_[i].z =( pointi.x * sin_2 +
  195.                               pointi.z * cos_2);
  196.                     save_rot = com;
  197.                     }
  198.     if(com == 0x32) {              /* spin x */
  199.                 point_[i].y =( pointi.y * cos_2  +
  200.                                pointi.z * sin_2);
  201.                 point_[i].z =( pointi.y * -sin_2 +
  202.                                pointi.z * cos_2);
  203.                      save_rot = com;
  204.                     }
  205.     if(com == 0x33) {              /* spin z */
  206.                 point_[i].x =( pointi.x * cos_2  +
  207.                                pointi.y * sin_2);
  208.                 point_[i].y =( pointi.y * cos_2  -
  209.                                pointi.x * sin_2);
  210.                      save_rot = com;
  211.                     }
  212.     if(com == 0x37) point_[i].x+=xoff;      /* These will move the */
  213.     if(com == 0x38) point_[i].y+=yoff;      /* object around in its */
  214.     if(com == 0x39) point_[i].z+=zoff;      /* OWN co-ord sys!! Thus */
  215.                                         /* changing these will effect*/
  216.                                         /* how it rotates about the origin */
  217.                                         /* The default is that the origin*/
  218.                                         /* is at the CENTER of the object */
  219.  
  220.    }
  221.       /* if perspective on/off picked */
  222. if( com < 2) 
  223.   {for(i=0;i<nlines;i++) draw3dline(drawpt1[i],drawpt2[i],0);/* erase */
  224.    if (com == 1) perspective = 1;
  225.    if (com == -1)perspective = 0;
  226.   }
  227.  
  228. if( (com > 0x77 && com < 0x7b) || ( com > 0x57 && com < 0x5b) ) 
  229.  {
  230.     for(i=0;i<nlines;i++) draw3dline(drawpt1[i],drawpt2[i],0);
  231.     if(com == 0x78) x_offset++;
  232.     if(com == 0x79) y_offset++;
  233.     if(com == 0x7a) z_offset+=0.2;
  234.     if(com == 0x58) x_offset--;
  235.     if(com == 0x59) y_offset--;
  236.     if(com == 0x5a) z_offset-=0.2;
  237.    com = 0x01; /* so that code below will execute */
  238.  } 
  239.  
  240.  
  241.  
  242. if(com == 0x31 || com == 0x32 || com == 0x33 || com == 0x01 || com == 0x37 || com == 0x38 || com ==0x39)          /* if 1,2,3 chosen, then display it */
  243.   for(i=0;i<nlines;i++)
  244.     draw3dline(drawpt1[i],drawpt2[i],0);  /*erase */
  245.   for(i=0;i<nlines;i++)
  246.      draw3dline(drawpt1[i]=point_[linefrom[i]],drawpt2[i]=point_[lineto[i]],color);
  247.      /* draw it */
  248.   
  249.  
  250. /*
  251. compute_normals();
  252. */
  253.  
  254.  
  255. com = save_rot;
  256.  
  257.  
  258.  
  259. }while( !(status & 0x2) );
  260.  
  261.  
  262. Setpalette(savepal);      /* restore palette */
  263.     theport -> plane0 = pl0;
  264.     theport -> plane1 = pl1;
  265.     theport -> plane2 = pl2;
  266.     theport -> plane3 = pl3;
  267.  
  268.  
  269.  
  270.  
  271.  
  272.     v_clsvwk(handle);
  273.     appl_exit();
  274.  
  275. }
  276.  
  277. /***********************************/
  278. int pick( x,y )
  279. int x,y;
  280.  
  281. {
  282.   if(y > 16 && y< 30) 
  283.     if( x > 515 && x < 538 ) return 0x32;
  284.     else if( x > 558 && x < 582 ) return 0x31;
  285.     else if( x > 603 && x < 626 ) return 0x33;
  286.   
  287.    if(y > 58 && y< 73) 
  288.     if( x > 530 && x < 553 ) {  return 1;}
  289.     else if( x > 581 && x < 604 ) {  return -1;}
  290.   
  291.   if(y > 58 && y< 73) 
  292.     if( x > 530 && x < 553 ) return 0x34;
  293.     else if( x > 581 && x < 604 ) return 0x34;
  294.  
  295.   if(y > 97 && y< 111) 
  296.     if( x > 528 && x < 564 ){ xoff = 1; return 0x37; }
  297.     else if( x > 565 && x < 603 ) { xoff = -1; return 0x37; }
  298.  
  299.   if(y > 113 && y< 127) 
  300.     if( x > 528 && x < 564 ){ yoff = 1; return 0x38; }
  301.     else if( x > 565 && x < 603 ) { yoff = -1; return 0x38; }
  302.    
  303.     if(y > 129 && y< 143) 
  304.     if( x > 528 && x < 564 ){ zoff = 1; return 0x39; }
  305.     else if( x > 565 && x < 603 ) { zoff = -1; return 0x39; }
  306.  
  307.    if(y > 163 && y< 177) 
  308.     if( x > 512 && x < 537 ) return 0x78;
  309.     else if( x > 554 && x < 578 ) return 0x79;
  310.     else if( x > 596 && x < 622 ) return 0x7a;
  311.  
  312.    if(y > 178 && y< 191) 
  313.     if( x > 512 && x < 538 ) return 0x58;
  314.     else if( x > 554 && x < 578 ) return 0x59;
  315.     else if( x > 596 && x < 622 ) return 0x5a;
  316.  
  317.   
  318. }
  319.  
  320.  
  321. draw3dline(p1,p2,color)
  322. point3d p1,p2;
  323. int color;
  324. {
  325.  int x1,y1,x2,y2;
  326.  
  327.  x1 = (int)( perx(p1) *z_offset) +x_offset;
  328.  y1 = (int)( pery(p1) *z_offset) +y_offset;
  329.  x2 = (int)( perx(p2) *z_offset) +x_offset;
  330.  y2 = (int)( pery(p2) *z_offset) +y_offset;
  331. /*
  332. printf(" %d,%d  %d,%d\n",x1,y1,x2,y2);
  333. */
  334.  if( x1 > 501) x1 = 501; if( x1 < 0 ) x1 = 0;
  335.  if( y1 > 199) y1 = 199; if( y1 < 0 ) y1 = 0;
  336.  if( x2 > 501) x2 = 501; if( x2 < 0 ) x2 = 0;
  337.  if( y2 > 199) y2 = 199; if( y2 < 0 ) y2 = 0;
  338.  
  339.  
  340.     
  341.     theport -> plane0 = color;
  342.   
  343.    a_line(x1,y1,x2,y2); 
  344.   
  345. }
  346.  
  347. mu(a,b)
  348. register long int a,b;
  349. {
  350.  asm
  351.     {
  352.      move.l a,D0
  353.      move.l b,D1
  354.      muls   D0,D1
  355.     }
  356. }
  357. di(a,b)
  358. register long int a,b;
  359. {
  360.  asm
  361.     {
  362.      move.l a,D0
  363.      move.l b,D1
  364.     }
  365. }
  366.  
  367.  
  368.  
  369. compute_normals()
  370. {
  371.  point3d A,B;
  372.  int h;
  373.   double An,Bn,ycomp;
  374.  
  375.  for(h=0;h<6;h++)
  376.    {
  377.     A.x = point_[to[h]].x - point_[from[h]].x;
  378.     A.z = point_[to[h]].z - point_[from[h]].z;
  379.     A.y = point_[to[h]].y - point_[from[h]].y;
  380.     B.x = point_[to[h+1]].x - point_[from[h+1]].x;
  381.     B.z = point_[to[h+1]].z - point_[from[h+1]].z;
  382.     B.y = point_[to[h+1]].y - point_[from[h+1]].y;
  383.  
  384.     An = sqrt(A.x*A.x+A.z*A.z+A.y*A.y);
  385.     Bn = sqrt(B.x*B.x+B.z*B.z+B.y*B.y);
  386.      A.x = A.x / An;
  387.     A.z = A.z / An;
  388.     B.x = B.x / Bn;
  389.     B.z = B.z / Bn;
  390.    
  391.     ycomp = A.z*B.x - A.x*B.z;
  392.     
  393.    }
  394.  
  395. }
  396.  
  397.  
  398. /************************************/
  399. /* load degas compressed pics */
  400.  
  401. /************************/
  402. read_stuff(hold,adrr,which)
  403. char hold[];
  404. register char *adrr;
  405. int which;  
  406. /* if which = 1 then store palette into newpal*/
  407. /* which is the main pal of the game. If = 0 then store */
  408. /* into pal2. the alternate pal of the game */
  409.  
  410. {
  411.  char buf[130];
  412.  int lines,i;
  413.  
  414.  
  415.  /*v_gtext(handle,1,5,"in read stuff");*/
  416.  
  417.  
  418.  
  419. filehandle = Fopen(hold,0); 
  420.  
  421. for(i=0; i<16;i++)
  422.  savepal[i]=Setcolor(i,-1);
  423. /* read header data */
  424. i=Fread(filehandle,2L,buf);
  425.  
  426.  
  427. /* read 16 words of palette data into newpal array */
  428. if(which==0) i = Fread(filehandle,32L,pal2); /* save pal2 */
  429. else
  430. i =Fread(filehandle,32L,newpal);
  431.  
  432.  
  433.  
  434.  
  435. i=Fread(filehandle,32000L,temp_);  /* read image onto back screen*/
  436. /* Close file */
  437. Fclose(filehandle);
  438.      lines = 200;                     /* Low, med-res    */
  439.                  iff_in  = temp_;                /* iff_in pts to temp_buf*/
  440.                  iff_out = adrr;      /* iff_out pts to pic_buffer*/
  441.                   do        
  442.          unpack(REZ);                                      /* Unpack a line at a time */
  443.                   while (--lines); 
  444.  
  445.  
  446.  /*v_gtext(handle,1,5,"             ");*/
  447.  
  448. }
  449. /************************/
  450.  
  451. /***********************/
  452.  
  453. /*---------------------------------------------------------------------------*/
  454. /*                             |--------- DEGAS ---------|            */
  455. /*                              UNCOMPRESSED   COMPRESSED            */
  456. /*                 NEO  low med mono   low med mono     TINY    */
  457. /*    typ...        0     1   2   3     4   5   6     7        */
  458.  
  459. /* Unpacks a single scan line & updates iff_in & iff_out global pointers
  460.  
  461.                      /    byt ==  0 to  127  copy next [byt+1] bytes
  462. Unpack routine --if-<    byt == -1 to -127  copy next byte [-byt+1] times
  463.                      \    byt == 128         NO-OP                            */
  464.  
  465. unpack(rez)
  466. int     rez;
  467.  
  468. {
  469.     register char     *src_ptr, *dst_ptr,           /* ptrs to source/dest */
  470.               byt, cnt;                     /* byt holds the ACTUAL compressed data code(control byte ) */
  471.     register int      minus128 = -128, 
  472.               len;                          
  473.     char                 linbuf[320];                            /* Oversize just in case! */
  474.     int                      llen;
  475.  
  476.  
  477.     if (rez < 2)     len = 160;
  478.     else             len = 80;
  479.     llen = len;
  480.     src_ptr = iff_in;           /* iff_in is ptr to compressed data */
  481.     dst_ptr = &linbuf[0];       /* linbuf WILL hold an ENTIRE Uncompressed scan line. 4 bitplanes * 80 = 320 max! */ 
  482.  
  483.     while (len > 0)
  484.    {
  485.             byt = *src_ptr++;       /* get byte value at address scr_ptr, THEN inc scr_ptr+1 */
  486.             if (byt >= 0)           /* If ctrl code >= 0 then use the next x+1 bytes*/
  487.     {
  488.                  ++byt;                 /* inc byt +1 */
  489.                   do 
  490.        {
  491.                         *dst_ptr++ = *src_ptr++;  /* get byte value from address source, and inc the 2 ptrs */
  492.                         --len;                    /* one byte down.. */
  493.                    }
  494.          while (--byt);           /* do this byt TIMES (remember byt here = byt+1 */
  495.              }
  496.              else 
  497.        if (byt != minus128)       /* else if ctrl code NOT = -128*/
  498.          {                        /*Then use the next byte -x+1 times, (-x) cause x will be negative and - - = + */
  499.                       cnt = -byt + 1;         /* cnt = -x + 1 */
  500.                       byt = *src_ptr++;       /* byt = THE very next byte past the ctrl code(or ctrl byte! */
  501.                        do {
  502.                                *dst_ptr++ = byt;  /* store that byte */
  503.                                --len;         
  504.                           }
  505.                while (--cnt);    /* keep doing it cnt times */
  506.                   }
  507.         }
  508.  
  509.     ilbm_st(linbuf, iff_out, rez);   /* convert the format line */
  510.     iff_in = src_ptr;                                    /* Update global pointers */
  511.     iff_out += llen;
  512.  
  513. }                                /* end of module uncompress() */
  514.  
  515. /*---------------------------------------------------------------------------*/
  516.  
  517. ilbm_st(src_ptr, dst_ptr, rez)           /* Convert ILBM format line to ST format */
  518. int         *src_ptr, *dst_ptr, rez;
  519. {
  520.     int         x, *p0_ptr, *p1_ptr, *p2_ptr, *p3_ptr;
  521.  
  522.     if (rez==0) 
  523.  {                                                     /* Low-res */
  524.   
  525.         p0_ptr = src_ptr;
  526.         p1_ptr = src_ptr + 20;
  527.         p2_ptr = src_ptr + 40;
  528.         p3_ptr = src_ptr + 60;
  529.         for (x=0; x<20; ++x)
  530.   {
  531.                *dst_ptr++ = *p0_ptr++;
  532.                *dst_ptr++ = *p1_ptr++;
  533.                *dst_ptr++ = *p2_ptr++;
  534.                *dst_ptr++ = *p3_ptr++;
  535.           }
  536.     } 
  537.  else if (rez==1) 
  538.   {                                            /* Med-res */
  539.            p0_ptr = src_ptr;
  540.            p1_ptr = src_ptr + 40;
  541.            for (x=0; x<40; ++x)
  542.     {
  543.                  *dst_ptr++ = *p0_ptr++;
  544.                  *dst_ptr++ = *p1_ptr++;
  545.             }
  546.     }
  547.     else 
  548.      {                                                   /* Monochrome */
  549.               for (x=0; x<40; ++x)
  550.                   *dst_ptr++ = *src_ptr++;
  551.          }
  552.  
  553.  }
  554. /*---------------------------------------------------------------------------*/
  555.  
  556.